package com.hr.edu.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hr.edu.domain.Education;
import com.hr.edu.service.impl.EduServiceImpl;
import com.hr.user.domain.User;
import com.hr.utils.BaseServlet;
import com.hr.utils.CommonUtils;
import com.hr.utils.PageBean;

public class EduServlet extends BaseServlet {
	private EduServiceImpl eduServiceImpl = new EduServiceImpl();
	/**
	 * 查询所有的培训项目
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryEduInfo(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		/**
		 * 1、获取页面传递的PageCurrent
		 * 2、给定PageSize的值
		 * 3、使用PageCurrent和PageSize调用Service方法，得到PageBean，保存request域
		 * 4、转发到/admin/dept_info.jsp
		 */
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		PageBean<Education> pageBean = eduServiceImpl.queryAll_Page(PageCurrent, PageSize);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		return "f:/edu/edu_list_info.jsp";
	}
	
	/**
	 * 查询参加当前培训的人员
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String descEduAllUser(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String edu_id = request.getParameter("edu_id");
		int PageCurrent = getPageCurrent(request);
		int PageSize = 5;
		PageBean<User> pageBean = eduServiceImpl.queryAll_Page_User_ByEducation(PageCurrent, PageSize, edu_id);
		pageBean.setUrl(getUrl(request));
		List<User> user_list = pageBean.getBeanList();
		Education education = eduServiceImpl.findByeduID(edu_id);
		for(int i=0;i<user_list.size();i++) {
			String state = eduServiceImpl.findState(education,user_list.get(i));
			user_list.get(i).setState(state);
		}
		pageBean.setBeanList(user_list);
		request.setAttribute("pb", pageBean);
		request.setAttribute("education", education);
		return "f:/edu/edu_list_user.jsp";
	}
	
	/**
	 * 获取当前的页面值
	 * @param request
	 * @return
	 */
	private int getPageCurrent(HttpServletRequest request){
		String value = request.getParameter("PageCurrent");
		if(value == null || value.trim().isEmpty()){
			return 1;
		} else{
			return Integer.parseInt(value);
		}
	}
	
	/**
	 * 截取url
	 * @param request
	 * @return
	 */
	public String getUrl(HttpServletRequest request){
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&PageCurrent=")) {
			int index = queryString.lastIndexOf("&PageCurrent=");
			queryString = queryString.substring(0, index);
		}
		
		return contextPath + servletPath + "?" + queryString;
	}
	
	/**
	 * 添加教育培训信息准备
	 * @param request
	 * @param response
	 * @return
	 */
	public String addEduInfo(HttpServletRequest request, HttpServletResponse response){
		return "f:/edu/edu_add.jsp";
	}
	
	/**
	 * 添加教育培训信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addEdu(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		Education education = CommonUtils.toBean(request.getParameterMap(), Education.class);
		education.setEdu_id(CommonUtils.uuid());
		eduServiceImpl.addEducation(education);
		return "f:/EduServlet?method=queryEduInfo";
	}
	
	/**
	 * 查询对应教育培训信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryEduPersonInfo(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		HttpSession session = (HttpSession) request.getSession();
		User user = (User)session.getAttribute("user");
		String user_id = user.getUser_id();
		int PageCurrent = getPageCurrent(request);
		int PageSize = 5;
		PageBean<Education> pageBean = eduServiceImpl.queryAll_Page_User_ByUserId(PageCurrent, PageSize, user_id);
		List<Education> education_list = pageBean.getBeanList();
		for(int i=0;i<education_list.size();i++){
			String state = eduServiceImpl.findState(education_list.get(i), user);
			education_list.get(i).setState(state);
		}
		pageBean.setBeanList(education_list);
		pageBean.setUrl(getUrl(request));
		request.setAttribute("pb", pageBean);
		request.setAttribute("user", user);
		return "f:/edu/edu_list_person.jsp";
	}
	
	/**
	 * 添加前准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addEduPersonPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		int PageCurrent = getPageCurrent(request);//获取当前页
		int PageSize = 4;//获取每页的记录数
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		PageBean<Education> pageBean = eduServiceImpl.queryAll_Page(PageCurrent, PageSize);
		pageBean.setUrl(getUrl(request));
		List<Education> education_list = pageBean.getBeanList();
		for(int i=0;i<education_list.size();i++){
			String state = eduServiceImpl.findState(education_list.get(i), user);
			education_list.get(i).setState(state);
		}
		pageBean.setBeanList(education_list);
		request.setAttribute("pb", pageBean);
		return "f:/edu/edu_add_person.jsp";
	}
	
	/**
	 * 添加申请信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addEduUser(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		HttpSession session = (HttpSession) request.getSession();
		String edu_id = request.getParameter("edu_id");
		User user = (User)session.getAttribute("user");
		String user_id = user.getUser_id();
		eduServiceImpl.addEducationUser(edu_id,user_id);
		return "f:/EduServlet?method=queryEduPersonInfo";
	}
	
	/**
	 * 放弃自己的培训资格
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteUserOwnEdu(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		HttpSession session = (HttpSession) request.getSession();
		String edu_id = request.getParameter("edu_id");
		User user = (User)session.getAttribute("user");
		eduServiceImpl.deleteUserOwnEdu(edu_id,user.getUser_id());
		return "f:/EduServlet?method=queryEduPersonInfo";
	}
	
	/**
	 * 修改教育培训项目准备
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String UpdateEduPre(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String edu_id = request.getParameter("edu_id");
		Education education = eduServiceImpl.findByeduID(edu_id);
		request.setAttribute("education", education);
		return "f:/edu/edu_edit.jsp";
	}
	
	/**
	 * 修改教育培训信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String updateEducation(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		Education education = CommonUtils.toBean(request.getParameterMap(), Education.class);
		eduServiceImpl.updateEducation(education);
		return "f:/EduServlet?method=queryEduInfo";
	}
	
	/**
	 * 删除教育培训项目
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteEdu(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String edu_id = request.getParameter("edu_id");
		eduServiceImpl.deleteEdu(edu_id);
		return "f:/EduServlet?method=queryEduInfo";
	}
	
	public String UpdateUserEduRelationShip(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String edu_id = request.getParameter("edu_id");
		String user_id = request.getParameter("user_id");
		eduServiceImpl.updateEducationUser(edu_id, user_id);
		return "f:/EduServlet?method=descEduAllUser&edu_id="+edu_id;
	}
	
	public String deleteUserEduRelationship(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String edu_id = request.getParameter("edu_id");
		String user_id = request.getParameter("user_id");
		eduServiceImpl.deleteUserEduRelationShip(edu_id,user_id);
		return "f:/EduServlet?method=descEduAllUser&edu_id="+edu_id;
	}
}
